package com.future.mayi.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.future.baselib.utils.ToastUtil;
import com.future.mayi.R;
import com.future.mayi.model.BrandModel;
import com.future.mayi.model.CateModel;
import com.future.mayi.model.CateTypeModel;
import com.future.mayi.model.QualityModel;
import com.future.mayi.model.SearchInfoModel;

import java.util.ArrayList;
import java.util.List;

public class SearchSelectView extends LinearLayout implements TypeItemView.TypeItemViewListener, View.OnClickListener {

    private Context mContext;


    private EditText edtPriceMin;
    private EditText edtPriceMax;


    private LinearLayout llContent;

    public SearchSelectView(Context context) {
        super(context);
        init(context);
    }

    public SearchSelectView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public SearchSelectView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private TypeViewListener listener;


    public interface TypeViewListener {
//        brand_id	否	int	品牌ID
//        cate_id	否	int	品类ID
//        quality_id	否	int	成色ID
//        is_sales	否	int	是否降价 0-否 1-是
//        is_credit	否	int	是否可分期 0-否 1-是

        void onTypeChange(String brand_id, String cate_id, String quality_id, int is_sales, int is_credit, int minPrice, int maxPrice);
    }


    private void init(Context context) {
        mContext = context;

        View.inflate(context, R.layout.layout_search_select, this);
        edtPriceMin = (EditText) findViewById(R.id.edt_price_min);
        edtPriceMax = (EditText) findViewById(R.id.edt_price_max);
        llContent = (LinearLayout) findViewById(R.id.ll_content);
        findViewById(R.id.tv_clear).setOnClickListener(this);
        findViewById(R.id.tv_ok).setOnClickListener(this);
        findViewById(R.id.view).setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_clear:
                clear();
                break;
            case R.id.tv_ok:
                search();
                break;
            case R.id.view:
                search();
                break;
        }
    }


    private void search() {
        int min = 0;
        int max = 0;
        if (!TextUtils.isEmpty(edtPriceMin.getText())) {
            min = Integer.valueOf(edtPriceMin.getText().toString());
        }

        if (!TextUtils.isEmpty(edtPriceMax.getText())) {
            max = Integer.valueOf(edtPriceMax.getText().toString());
        }

        if (min > 999999999) {
            ToastUtil.getInstance().show(mContext, "最低价超过输入最大限制，请修改");
            return;
        }

        if (max > 999999999) {
            ToastUtil.getInstance().show(mContext, "最高价超过输入最大限制，请修改");
            return;
        }

        if (min != 0 && max != 0 && min > max) {
            edtPriceMax.setText(String.valueOf(min));
            edtPriceMin.setText(String.valueOf(max));
            int cache = min;
            min = max;
            max = cache;
        }

        setVisibility(GONE);


        int is_sales = 0;
        int is_credit = 0;
        String other = otherView.getCateId();
        if ((!TextUtils.isEmpty(other))) {
            if (other.contains("88888"))
                is_sales = 1;
            if (other.contains("99999"))
                is_credit = 1;
        }

        if (listener != null)
            listener.onTypeChange(brandView.getCateId(), cateView.getCateId(), qualityView.getCateId(), is_sales, is_credit, min, max);
    }

    TypeItemView brandView;
    TypeItemView cateView;
    TypeItemView qualityView;
    TypeItemView otherView;

    public void setData(SearchInfoModel searchInfoModel) {

        llContent.removeAllViews();

        brandView = new TypeItemView(mContext);
        llContent.addView(brandView);
        brandView.setListener(this);
        CateTypeModel brandCateTypeModel = new CateTypeModel();
        brandCateTypeModel.setTitle("品牌");
        List<CateTypeModel.CateSonModel> brandList = new ArrayList<>();
        for (BrandModel brandModel : searchInfoModel.getBrand_list()) {
            CateTypeModel.CateSonModel model = new CateTypeModel.CateSonModel();
            model.setTitle(brandModel.getBrand_name());
            model.setCate_id(brandModel.getBrand_id());
            brandList.add(model);
        }
        brandCateTypeModel.setCate_son(brandList);
        brandView.setData(brandCateTypeModel);


        cateView = new TypeItemView(mContext);
        llContent.addView(cateView);
        cateView.setListener(this);
        CateTypeModel cateTypeModel = new CateTypeModel();
        cateTypeModel.setTitle("品类");
        List<CateTypeModel.CateSonModel> cateList = new ArrayList<>();
        for (CateModel cateModel : searchInfoModel.getCate_list()) {
            CateTypeModel.CateSonModel model = new CateTypeModel.CateSonModel();
            model.setTitle(cateModel.getCate_name());
            model.setCate_id(cateModel.getCate_id());
            cateList.add(model);
        }
        cateTypeModel.setCate_son(cateList);
        cateView.setData(cateTypeModel);


        qualityView = new TypeItemView(mContext);
        llContent.addView(qualityView);
        qualityView.setListener(this);
        CateTypeModel qualityCateTypeModel = new CateTypeModel();
        qualityCateTypeModel.setTitle("成色");
        List<CateTypeModel.CateSonModel> qualityList = new ArrayList<>();
        for (QualityModel qualityModel : searchInfoModel.getQuality_list()) {
            CateTypeModel.CateSonModel model = new CateTypeModel.CateSonModel();
            model.setTitle(qualityModel.getQuality_name());
            model.setCate_id(qualityModel.getQuality_id());
            qualityList.add(model);
        }
        qualityCateTypeModel.setCate_son(qualityList);
        qualityView.setData(qualityCateTypeModel);


        otherView = new TypeItemView(mContext);
        llContent.addView(otherView);
        otherView.setListener(this);
        CateTypeModel otherCateTypeModel = new CateTypeModel();
        otherCateTypeModel.setTitle("降价/分期");
        List<CateTypeModel.CateSonModel> otherList = new ArrayList<>();

        CateTypeModel.CateSonModel model = new CateTypeModel.CateSonModel();
        model.setTitle("有降价");
        model.setCate_id(88888);
        otherList.add(model);

        CateTypeModel.CateSonModel model1 = new CateTypeModel.CateSonModel();
        model1.setTitle("支持分期");
        model1.setCate_id(99999);
        otherList.add(model1);

        otherCateTypeModel.setCate_son(otherList);
        otherView.setData(otherCateTypeModel);


    }

    public void setListener(TypeViewListener listener) {
        this.listener = listener;
    }


    @Override
    public void onTypeChange() {

    }

    public void selectBrand(int brandId) {
        if (brandView == null) return;
        brandView.selectId(brandId);
        search();
    }


    public boolean selectStyle(int styleId) {
        if (brandView == null) return false;
        if (cateView.selectId(styleId)) {
            search();
            return true;
        } else return false;
    }

    public void selectFq() {
        otherView.selectId(99999);
        search();
    }


    public void clear() {

        brandView.clear();
        cateView.clear();
        qualityView.clear();
        otherView.clear();
        edtPriceMin.setText("");
        edtPriceMax.setText("");
    }
}
